package com.canbot.u05.activity.presidemode.newcode;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.VoiceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.control.DataUtil;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;

import butterknife.BindView;

public class NChoiceTtsActivity extends BaseHostActivity implements View.OnClickListener {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.et_tts_content)
    EditText etTtsContent;
    @BindView(R.id.tv_cost_time)
    TextView tvCostTime;
    @BindView(R.id.tv_try_play)
    TextView tvTryPlay;
    @BindView(R.id.showTime)
    TextView showTime;

    @Override
    protected void initEventData() {
        initCommonHead();
        calculateTime();
        tvCostTime.setOnClickListener(this);
        tvTryPlay.setOnClickListener(this);
        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            VoiceBean voiceBean = (VoiceBean) intent.getSerializableExtra(DATA);
            if (voiceBean != null && voiceBean.languageType == NModuleType.VOICE_TTS) {
                etTtsContent.setText(voiceBean.content);
                etTtsContent.setSelection(voiceBean.content.length());
            }
        }
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    String content = etTtsContent.getText().toString();
                    if (!TextUtils.isEmpty(content)) {
                        VoiceBean voiceBean = new VoiceBean(NModuleType.VOICE_TTS);
                        voiceBean.content = content;
                        Intent intent = new Intent();
                        intent.putExtra(DATA, voiceBean);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Toast.makeText(this, getString(R.string.tts_is_null), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nchoice_tts;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_cost_time:
                calculateTime();
                break;
            case R.id.tv_try_play:
                play();
                break;
        }
    }

    private void play() {
        String content = etTtsContent.getText().toString();
        if (!TextUtils.isEmpty(content)) {
            MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, content);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MapVoiceAndEyes.stop();
    }

    private void calculateTime() {
        String content = etTtsContent.getText().toString();
        String string = getString(R.string.search_time);
        String format = String.format(string, DataUtil.getTtsTime(content));
        showTime.setText(format);
    }

}
